package com.coderman.controller.business;

import com.coderman.business.service.IExamTypeService;
import com.coderman.common.annotation.ControllerEndpoint;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.student.ExamType;
import com.coderman.common.response.ResponseBean;
import com.coderman.common.vo.student.ExamTypeVO;
import com.coderman.common.vo.system.PageVO;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "考试项目管理")
@RestController
@RequestMapping("/project")
/**
 * 考试项目控制层
 * @auther 文晓平
 * @version 1.0
 * @date 2023-1-9
 */
public class ExamTypeController {

    @Autowired
    private IExamTypeService examTypeService;

    /**
     * 考试项目列表
     *
     * @return
     */
    @ApiOperation(value = "考试项目列表", notes = "模糊查询考试项目列表")
    @GetMapping("/findList")
    @ResponseBody
    public ResponseBean<PageVO<ExamType>> findList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                   ExamTypeVO examTypeVO) {
        PageVO<ExamType> examTypeList = examTypeService.findList(pageNum, pageSize, examTypeVO);
        return ResponseBean.success(examTypeList);
    }


    /**
     * 添加考试项目
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加考试项目失败", operation = "添加考试项目")
    @RequiresPermissions({"examtype:add"})
    @ApiOperation(value = "添加考试项目")
    @PostMapping("/add")
    @ResponseBody
    public ResponseBean add(@RequestBody @Validated ExamTypeVO examTypeVO) {
        examTypeService.add(examTypeVO);
        return ResponseBean.success();
    }

    /**
     * 编辑考试项目
     *
     * @param id 编号
     * @return
     */
    @ApiOperation(value = "编辑考试项目")
    @RequiresPermissions({"examtype:edit"})
    @GetMapping("/edit/{id}")
    @ResponseBody
    public ResponseBean edit(@PathVariable Integer id) throws SystemException {
        ExamTypeVO examTypeVO = examTypeService.edit(id);
        return ResponseBean.success(examTypeVO);
    }

    /**
     * 更新考试项目操作
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新考试项目失败", operation = "更新考试项目")
    @ApiOperation(value = "更新考试项目")
    @RequiresPermissions({"examtype:update"})
    @PutMapping("/update/{id}")
    @ResponseBody
    public ResponseBean update(@PathVariable Integer id, @RequestBody @Validated ExamTypeVO examTypeVO) throws SystemException {
        examTypeService.update(id, examTypeVO);
        return ResponseBean.success();
    }

    /**
     * 删除考试项目
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除考试项目失败", operation = "删除考试项目")
    @ApiOperation(value = "删除考试项目")
    @RequiresPermissions({"examtype:delete"})
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResponseBean delete(@PathVariable Integer id) throws SystemException {
        examTypeService.delete(id);
        return ResponseBean.success();
    }




    /**
     * 导出excel
     * @param response
     */
    @ApiOperation(value = "导出excel", notes = "导出所有考试项目的excel表格")
    @PostMapping("/excel")
    @RequiresPermissions("examtype:export")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败",operation = "导出考试项目excel")
    public void export(HttpServletResponse response) {
        List<ExamType> examTypes = this.examTypeService.findAll();
        ExcelKit.$Export(ExamType.class, response).downXlsx(examTypes, false);
    }

    /**
     * 所有考试信息
     *
     * @return
     */
    @ApiOperation(value = "所有考试项目")
    @GetMapping("/findAll")
    @ResponseBody
    public ResponseBean<List<ExamTypeVO>> findAll() {
        List<ExamTypeVO> examTypeVOS = examTypeService.findAllVO();
        return ResponseBean.success(examTypeVOS);
    }


}
